package org.reborn.utils.niobuffer

import java.nio.{Buffer,ByteBuffer,ByteOrder,FloatBuffer,IntBuffer,CharBuffer,ShortBuffer,LongBuffer,DoubleBuffer}

class ResizeableFloatBuffer(initialCapacity:Int,resizeAmount:Int){
  private var buffer:FloatBuffer = BufferUtil.createDirectFloatBuffer(initialCapacity)
  private var bufferLength:Int = 0 
  
  def getBuffer = {
    buffer.limit(bufferLength)
    buffer.position(0)
    buffer.slice()
  }
  def getLength = bufferLength
  
  def append(value:Float) = {
    resizeBuffer(1)
    buffer.put(bufferLength,value)
    bufferLength += 1
  }
  def append(data:FloatBuffer) = {
    data.rewind
    buffer.position(bufferLength)
    resizeBuffer(data.capacity)
    buffer.put(data)
    bufferLength += data.capacity
  }
  
  def replace(data:FloatBuffer) = {
    data.rewind
    buffer = BufferUtil.createDirectFloatBuffer(data.capacity + resizeAmount)
    buffer.rewind
    buffer.put(data)
    bufferLength = data.capacity
  }
  
  private def resizeBuffer(additionalSize:Int){
    buffer = BufferUtil.resizeBuffer(buffer,bufferLength,additionalSize)
  }
}

class ResizeableIntBuffer(initialCapacity:Int,resizeAmount:Int){
  private var buffer:IntBuffer = BufferUtil.createDirectIntBuffer(initialCapacity)
  private var bufferLength:Int = 0 
  
  def getBuffer = {
    buffer.limit(bufferLength)
    buffer.position(0)
    buffer.slice()
  }
  def getLength = bufferLength
  
  def append(value:Int) = {
    resizeBuffer(1)
    buffer.put(bufferLength,value)
    bufferLength += 1
  }
  def append(data:IntBuffer) = {
    data.rewind
    buffer.position(bufferLength)
    resizeBuffer(data.capacity)
    buffer.put(data)
    bufferLength += data.capacity
  }
  
  def replace(data:IntBuffer) = {
    data.rewind
    buffer = BufferUtil.createDirectIntBuffer(data.capacity + resizeAmount)
    buffer.rewind
    buffer.put(data)
    bufferLength = data.capacity
  }
  
  private def resizeBuffer(additionalSize:Int){
    buffer = BufferUtil.resizeBuffer(buffer,bufferLength,additionalSize)
  }
}